//   ---------------模块化 引用js文件-------------------

import Cookie from './cookie.js'
import Api from  './api.js'
import WApi from  '../wj/api.js'
import Base_api from './Base_api.js'
import Basefunction from './Basefunction.js'

//    -------------------全局配置-----------------------
let url="http://localhost:8080"
$.ajaxSetup({
    async:false,
})

//   --------------------分页功能------------------------
let pageIndex=1;        //第几页
let pageSize=3;         //一页多少数据
let food = ''
let place = ''
let jcrs = ''

new Vue({
    el:'#active',
    data(){
        return{
            sera:'',
            Maxtime:"",                   //火热饭局时间
            Maxuser:[],                   //火热饭局人
            Maxactive:[],                 //火热的饭局
            zixun:[],                     //热门活动资讯
            endactivity:[],               //活动结束的状态位
            uinfos:[],                   //当前用户
            search:'',                  //------搜索框内容
            types:[],                   //饭局类型
            pickerOptions:{
                disabledDate(time) {
                    return time.getTime() < Date.now();
                }
            },
            places:[],                  //饭局地点
            //--------------------分页内容---------------------
            currentPage4: pageIndex  ,        //当前页数
            activityShows:[],         //显示的饭局自建类,
            pageSize:pageSize,        //数据显示条数
            tags:[],                  //活动标签
            choseid:0,               //报名选择的饭局id
            size:pageSize,          //每页多少条
            count:'',               //总共饭局数
            //----------------查询的部分组件-----------------
            type:[],                    //类别查询
            value1:'',                 //开始时间
            place:[],                  //地点查询
            num:'',                    //人数
            tag:[0,1,2,3,4,5],                     //  默认全部查询
            temp:1,
        }
    },
    methods:{
        tiaozhuan(items){
          sessionStorage.setItem('item',JSON.stringify(items))

            location.href = './detail.html'
        },
        async getserach() {
            if(this.search !=''){
                let activitys = await Base_api.getserach(this.search, Base_api.getActivityAll222())
                this.activityShows = activitys
                this.getDate123()
                return
            }
            if(food !='' || place !='' || jcrs !=''){
                let activitys = await Base_api.getserach(this.sera, Base_api.getActivityAll222())
                if(place !='' || jcrs !=''){
                    let activitys2 = []
                    activitys2 = await Base_api.getserach(place, activitys)
                    if(activitys2.length>0){
                        let activitys3 = []
                        let k = 0
                        // alert(JSON.stringify(activitys2))
                        for(let i=0;i<activitys2.length;i++){
                            if(jcrs <= activitys2[i].activity.max_people){
                                activitys3[k] = activitys2[i]
                                k++
                            }
                        }
                        // alert('jcrs')
                        this.activityShows = activitys3
                        this.getDate123()
                        return
                    }
                    // alert('place')
                    this.activityShows = activitys2
                    this.getDate123()
                    return
                }
            // alert('food')
                this.activityShows = activitys
                this.getDate123()
                return
            }
        },
        //获取个人信息
        async info () {
            let zh = sessionStorage.getItem("zh")
            let allinfo = await WApi.login()
            for(let i=0;i<allinfo.length;i++){
                if(allinfo[i].zh==zh){
                    this.uinfos=allinfo[i]
                    break
                }
            }
            food = Base_api.getUrlParms("food")
            place = Base_api.getUrlParms("place")
            jcrs = Base_api.getUrlParms("jcrs")
            if(food !='' || place !='' || jcrs !=''){
                if(food != ''){
                    this.sera = food
                    this.getserach()
                }else {
                    if(place != ''){
                        this.sera = place
                        this.getserach()
                    }else {
                        if(jcrs != ''){
                            this.sera = jcrs
                            this.getserach()
                        }
                    }
                }
            }else {
                this.activityShows=getActivityShowPaging("","","","","",1)
            }
        },
        getDate123(){                                              //活动是否已结束,已结束的就不能显示出来
            for (let i=0;i<this.activityShows.length;i++){
                var strDate=new Date(this.activityShows[i].activity.end_time)
                var newdate = new Date()
                if(newdate < strDate){
                    this.endactivity[i]=true
                }else {
                    this.endactivity[i]=false
                }
            }
        },
        handleSizeChange(val) {
            this.$message({
                message: `每页 ${val} 条`,
                type: 'success'
            });
            pageSize=val
            let activitys=getActivityShowPaging("","","","","",this.tag[this.temp])
            this.activityShows=activitys
            this.getDate123()
        },
       handleCurrentChange(val) {
            this.$message({
                message: `当前页: ${val}`,
                type: 'success'
            });
            pageIndex = val
            let activitys = getActivityShowPaging("", "", "", "", "", this.tag[this.temp])
            this.activityShows = activitys
            this.getDate123()
        },
        baoming(obj) {
            // alert(JSON.stringify(obj))
            let cid = obj.id
            Cookie.setCookie("bm",obj.id,'d1')
            Cookie.setCookie("maxpeople",obj.max_people,'d1')
            window.location.href='./Sign_up.html'
        },
        changeLink(index,type){
            let a = document.getElementById(index)
            if(a.className=="baseStyles"){
                a.className="overStyles"
                this.type.push(type)
            }else if(a.className=="overStyles"){
                a.className="baseStyles"
                this.type.pop(type)
            }
        },
        changePlace(index,place){
            let a = document.getElementById(index)
            if(a.className=="baseStyles"){
                a.className="overStyles"
                this.place.push(place)
            }else if(a.className=="overStyles"){
                a.className="baseStyles"
                this.place.pop(place)
            }
        },
        softGruopAll(){
            this.activityShows=getActivityShowPaging("","","","","",1)
            this.getDate123()
        },
        softGruopTime(){
            let time = document.getElementById("time")
            let t1 = document.getElementById("t1")
            if(time.onclick==null){
                if(time.title=="时间从远到近"){
                    this.activityShows=getActivityShowPaging("","","","","",2)
                    time.title="时间从近到远"
                    this.getDate123()
                    this.temp=2
                }else if(time.title=="时间从近到远"){
                    this.activityShows=getActivityShowPaging("","","","","",3)
                    time.title="时间从远到近"
                    this.getDate123()
                    this.temp=3
                }
            }
        },
        softGruopMoney(){
            let money = document.getElementById('money')
            let t2 = document.getElementById("t2")
            if(money.onclick==null){
                if(money.title=="菜金从高到低"){
                        this.activityShows=getActivityShowPaging("","","","","",4)
                    money.title="菜金从低到高"
                    this.getDate123()
                        this.temp=4
                    }else if(money.title=="菜金从低到高"){
                        this.activityShows=getActivityShowPaging("","","","","",5)
                    money.title="菜金从高到低"
                    this.getDate123()
                    this.temp=5
                }
            }
        },
        selectTo(){
            if(this.type.length==0 || this.place.length==0 || this.value1==null || this.value1 =='') {
                this.$alert('请把信息选择好,不然就不要按查询', '温馨提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$message({
                            type: 'warning',
                            message: '信息不能缺少任何一项,否则查询不到！！!'
                        });
                    }
                });
                if(this.type.length>1) {
                    this.$alert('饭局类型只能选择一个,因为麻烦不好查找', '温馨提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.$message({
                                type: 'warning',
                                message: '饭局的类型必须明确,才能查询得到！！!'
                            });
                        }
                    });
                }
                if(this.value1==null || this.value1 =='') {
                    this.$alert('饭局时间不能为空', '温馨提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.$message({
                                type: 'warning',
                                message: '饭局的时间必须明确,才能查询得到！！!'
                            });
                        }
                    });
                }
                if(this.place.length >1){
                    this.$alert('地点只能选择一个', '温馨提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.$message({
                                type: 'warning',
                                message: '吃饭的地点必须明确,才能查询得到！！!'
                            });
                        }
                    });
                }
            }
            else{
                let dates = Basefunction.getTag(this.value1.length,this.value1)
                let begin_time=dates[0]
                let end_time=dates[1]
                this.activityShows=getActivityShowPaging(this.type,begin_time,end_time,this.place,this.num,0)
                this.getDate()
            }
        },
        getMaxpeople(){
            this.Maxactive = Base_api.getMaxactive()
            this.Maxtime=this.Maxactive[0].c_time.substring(0,16)
            this.Maxuser=Api.login(this.Maxactive[0].push_id)
            // alert(JSON.stringify(this.Maxactive))
            let temp
            if( this.Maxactive==null ||this.Maxactive==[]){
                this.Maxactive=(Base_api.getActivityAll())[0]
                this.Maxtime=this.Maxactive[0].c_time.substring(0,16)
                this.Maxuser=Api.login(this.Maxactive[0].push_id)
            }else {
                for(let i=0;i<this.Maxactive.length;i++){
                    var strDate=this.Maxactive[i].end_time
                    var newdate = new Date()
                    if(newdate < new Date(strDate)){
                        temp=this.Maxactive[i]
                        this.Maxtime=this.Maxactive[i].c_time.substring(0,16)
                        this.Maxuser=Api.login(this.Maxactive[i].push_id)
                        this.Maxactive=temp
                    }
                }
            }
            // alert(JSON.stringify(this.Maxactive))
        },
        setzixun(){
            this.zixun=getActivityShowPaging("","","","","",2)
            // alert(this.zixun[0].activity.toString())
        }
    },
   mounted() {
        //------加载页面初始化数据
       if(sessionStorage.getItem("zh")==null){
           this.$alert('请先登录', '系统提示', {
               confirmButtonText: '确定',
               callback: action => {
                   this.$message({
                       type: 'warning',
                       message: '即将跳转到登录界面...'
                   });
                   location.href='./login.html'
               }
           });
       }else{
           this.info()
           this.types=Base_api.getTypes()
           this.places=Base_api.getPlaces()

           this.count=Base_api.getActivityAll().length
           this.getDate123()
           this.setzixun()
           this.getMaxpeople()
       }
    }
})


// ---------------分页功能-----------------------------------
// 获取分页显示数据
function getActivityShowPaging(typeP,c_time,e_time,placeP,numP,tagP) {
    let activitys
    $.post(url+'/ActivityServlet',{
        type:"pagaingActivity",
        pageIndex:pageIndex,
        pageSize:pageSize,
        lb_name:typeP.toString(),
        c_time:c_time,
        end_time:e_time,
        address:placeP.toString(),
        people:numP,
        tag:tagP
    },function (result) {
        activitys=result.data
        console.log(activitys)
    },"json")
    return activitys
}
// ----------------------------------------------------------







